package com.example.administrator.basepro.ui.act;

import android.Manifest;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.StringUtils;
import com.example.administrator.basepro.App;
import com.example.administrator.basepro.R;
import com.example.administrator.basepro.base.BaseActivity;
import com.example.administrator.basepro.dagger.component.DaggerActivityComponent;
import com.example.administrator.basepro.model.bean.LoginBean;
import com.example.administrator.basepro.ui.contract.LoginContract;
import com.example.administrator.basepro.ui.presenter.LoginPresenter;
import com.example.administrator.basepro.utils.ActivityUtils;
import com.example.administrator.basepro.utils.ToastUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;
import retrofit2.http.PUT;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View{


    @BindView(R.id.et_password)
    public EditText etPassword;

    @BindView(R.id.et_username)
    public EditText etUsername;

    @BindView(R.id.btn_login)
    public Button btnLogin;

    @BindView(R.id.btn_register)
    public Button btnRegister;

    @BindView(R.id.des)
    public TextView des;


    @BindView(R.id.btn_real_register)
    public Button btn_real_register;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initEventAndData() {
        des.setText("登录");
        btnLogin.setVisibility(View.VISIBLE);
        btnRegister.setVisibility(View.VISIBLE);
        btn_real_register.setVisibility(View.GONE);
        RxPermissions rxPermissions=new RxPermissions(this);
        rxPermissions.request(Manifest.permission.INTERNET,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean granted) {
                        if (granted) {

                        } else {
                        }
                    }
                });
    }

    @Override
    public void showLoginInfo(LoginBean loginBean) {
        if (loginBean.isSuccess()){
            Toast.makeText(mContext, "登录成功!", Toast.LENGTH_SHORT).show();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    ActivityUtils.startActivity(MainActivity.class);
                    finish();
                }
            },500);
        }else {
            Toast.makeText(mContext, "登录失败:"+loginBean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showLoginError(String error) {

    }

    @Override
    public void showRegisterInfo(LoginBean loginBean) {
        if (loginBean.isSuccess()){
            Toast.makeText(mContext, "注册成功，登录吧!", Toast.LENGTH_SHORT).show();
            btnLogin.setVisibility(View.VISIBLE);
            btnRegister.setVisibility(View.VISIBLE);
                btn_real_register.setVisibility(View.GONE);
        }else {
            Toast.makeText(mContext, "注册失败:"+loginBean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }


    @OnClick({R.id.btn_login,R.id.btn_register,R.id.btn_real_register})
    public void onclick(View view){
        String pass = etPassword.getText().toString();
        String name = etUsername.getText().toString();
        switch (view.getId()){
            case R.id.btn_login:
               if (!checkValid(name,pass))return;
                mPresenter.login(name,pass);
                break;
            case R.id.btn_register:
                des.setText("请注册");
                btnLogin.setVisibility(View.GONE);
                btnRegister.setVisibility(View.GONE);
                btn_real_register.setVisibility(View.VISIBLE);
                break;

            case R.id.btn_real_register:
                if (!checkValid(name,pass))return;
                mPresenter.register(name,pass);
                break;
        }
    }

    public boolean checkValid(String name ,String pass) {

        if (StringUtils.isEmpty(name)){
            Toast.makeText(mContext, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (StringUtils.isEmpty(pass))
        {
            Toast.makeText(mContext, "密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;

    }
}
